/**
 * Created by Administrator on 2017/3/26.
 */
var wnDrawingPolygon = {
    getMap: function(id, listPonit,center) {
        var mapProp = {
            center: new google.maps.LatLng(center.lat, center.lng),
            zoom: 16,
            mapTypeId: google.maps.MapTypeId.HYBRID
        };
        var map = new google.maps.Map(document.getElementById(id), mapProp);
        return map;
    },
    getDrawingManager: function() {
        var drawingManager = new google.maps.drawing.DrawingManager({
            drawingMode: google.maps.drawing.OverlayType.POLYGON,
            drawingControl: false,
            drawingControlOptions: {
                position: google.maps.ControlPosition.TOP_CENTER,
                drawingModes: [google.maps.drawing.OverlayType.MARKER, google.maps.drawing.OverlayType.POLYGON, google.maps.drawing.OverlayType.POLYLINE, google.maps.drawing.OverlayType.RECTANGLE]
            },
            markerOptions: {
                icon: '/static/image/map/farmland.png'
            },
            infoWindowOptions: {
                disableAutoPan: true
            },
            //设置图形显示样式
            polygonOptions: {
                strokeColor: "#FF0000",
                //描边的颜色
                strokeOpacity: 0.8,
                //描边的透明度
                strokeWeight: 1,
                //描边的线宽
                fillColor: null,
                //填充的颜色
                fillOpacity: 0.4,
                //填充的透明度
                editable: true,
                //允许编辑
                zIndex: google.maps.Marker.MAX_ZINDEX + 3
            }
        });
        return drawingManager;
    },
    setEvent: function(drawingManager,map,calback) {
        google.maps.event.addListener(drawingManager, 'polygoncomplete',
            function(event) {
                var bounds = new google.maps.LatLngBounds();

                var paths = event.getPath();
                var arr= paths.getArray();
                for(var i=0; i<arr.length;i++){
                    bounds.extend(arr[i]);
                };
                var cent = new google.maps.Marker({
                    position: bounds.getCenter(),
                    icon: "/static/image/map/farmland.png"
                });
                var obj ={
                    bounds : bounds,
                    paths:paths,
                    drawingManager:drawingManager,
                    map:map,
                    calback:calback,
                    cent:cent,
                    isEidt:false

                }
                google.maps.event.addListener(paths, 'insert_at',function(index){
                    obj.isEidt=true;
                    drawEventPolygoncomplete(obj);
                } );
                google.maps.event.addListener(paths, 'remove_at',function(index){
                    obj.isEidt=true;
                    drawEventPolygoncomplete(obj);
                });
                google.maps.event.addListener(paths, 'set_at', function(index){
                    obj.isEidt=true;
                    drawEventPolygoncomplete(obj);
                });
                drawEventPolygoncomplete(obj);

            });
    }
}

function drawEventPolygoncomplete(obj) {
    obj.cent.setMap(null);
    for(var i=0; i<obj.paths.getArray().length;i++){
        obj.bounds.extend(obj.paths.getArray()[i]);
    };
    if( obj.isEidt==true){

    }else{
        obj.isEidt==true
    }
    obj.cent=new google.maps.Marker({
        position: obj.bounds.getCenter(),
        icon: "/static/image/map/farmland.png"
    });
    obj.cent.setMap(obj.map);
    obj.drawingManager.setDrawingMode(null);
    var mianji = google.maps.geometry.spherical.computeArea(obj.paths)*0.0015;
    obj.calback( obj.paths.getArray(), obj.bounds.getCenter(),mianji);
}